package tool.prototypes;

import java.util.ArrayList;
import java.util.List;

import tool.codescan.mappers.PackageMapper;
import tool.data.FieldRecord;
import tool.data.FieldRecord.EScope;
import tool.data.ParameterRecord;
import japa.parser.ast.body.FieldDeclaration;
import japa.parser.ast.body.Parameter;
import japa.parser.ast.expr.VariableDeclarationExpr;
import japa.parser.ast.type.ClassOrInterfaceType;
import japa.parser.ast.type.PrimitiveType;
import japa.parser.ast.type.ReferenceType;
import japa.parser.ast.type.Type;
import japa.parser.ast.type.VoidType;
import japa.parser.ast.type.WildcardType;
import japa.parser.ast.visitor.VoidVisitorAdapter;

	/**
	 * @author jwb09119
	 * @date 2013/11/21
	 * 
	 * This visitor is intended to visit every class variable and method parameter and identify the
	 * types of the fields declared.  This should allow the identification of Interface use 
	 * patterns.
	 * 
	 * @deprecated
	 * Replaced by individual visitors for each type or entity, now located in
	 * src.tool.codescan.visitors
	 * 
	 * @date 2013/12/03
	 * Any code causing compilation errors due to non-compatibility with new data structures or
	 * interfaces has been commented out.	 
	 */

public class OldVariableVisitor<A> extends VoidVisitorAdapter<A>{

	private PackageMapper packageMap;
	private List<FieldRecord> fields;
	private List<ParameterRecord> params;
	
	public OldVariableVisitor (PackageMapper mapper) {
		
		this.packageMap = mapper;
		fields = new ArrayList<>();
		params = new ArrayList<>();
		
	}
	
	
//    @Override
//    public void visit(Parameter n, A arg) {
//    	System.out.println("\nParameter: \n" + n.toString());
//    	
//    	ParameterRecord pr = new ParameterRecord();
//    	pr.setName(n.getId().getName());
//    	pr.setFullTypeName(getTypeName(n.getType()));
//    	pr.setIsArray((n.getId().getArrayCount() > 0));
//    	params.add(pr);
//    	
//    }
    
//    @Override
//    public void visit(FieldDeclaration n, A arg) {
//    	System.out.println("\nField: \n" + n.toString());
//    	
//    	FieldRecord fr = new FieldRecord(EScope.CLASS);
//    	fr.setName(n.getVariables().get(0).getId().getName());
//    	fr.setFullTypeName(getTypeName(n.getType()));
//    	fr.setArray((n.getVariables().get(0).getId().getArrayCount() > 0));
//    	fr.setModifiers(n.getModifiers());
//    	fields.add(fr);
//
//    }
    
    
//    @Override
//    public void visit(VariableDeclarationExpr n, A arg) {
//    	System.out.println("\nVariable Declaration: \n" + n.toString());
//    	
//    	FieldRecord fr = new FieldRecord(EScope.METHOD);
//    	fr.setName(n.getVars().get(0).getId().getName());
//    	fr.setFullTypeName(getTypeName(n.getType()));
//    	fr.setArray((n.getVars().get(0)).getId().getArrayCount() > 0);
//    	fr.setModifiers(n.getModifiers());
//    	fields.add(fr);
//    	
//    }
    
    
    private String getTypeName (Type type) {
    	
    	String reply = "TYPE NOT RESOLVED";
    	
    	if (type instanceof ClassOrInterfaceType) {
    		
    		reply =  ((ClassOrInterfaceType) type).getName();
    		
    	} else if (type instanceof PrimitiveType) {
    		
    		reply = ((PrimitiveType) type).getType().toString();
    		
    	} else if (type instanceof ReferenceType) {
    		
    		reply = getTypeName(((ReferenceType) type).getType());
    		
    	} else if (type instanceof VoidType) {
    		
    		reply = "VOID";
    		
    	} else if (type instanceof WildcardType) {
    		
    		reply = ((WildcardType) type).toString();
    		
    	}
    	
    	return reply;
    } 

    
    public void printSummary () {
    	
    	System.out.println("\nInterface Use inside types:");
    	System.out.println("Fields Found: " + fields.size());
    	System.out.println("Parameters Found: " + params.size());
    	
    }
    
    
	public void printDetailedSummary() {
		
		printSummary();
		
		System.out.println("Variable Details:");
		
		for (FieldRecord f : fields) {
			System.out.println(f);
		}
		
		for (ParameterRecord p : params) {
			System.out.println(p);
		}
		
	}
	
}
